home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / HideMenubarEtc / ShowHideDemo.c < prev    next >
Encoding:
Text File  |  1995-06-16  |  12.1 KB  |  394 lines  |  [TEXT/MMCC]

  1. //
  2. //    Application:    ShowHideDemo
  3. //
  4. //    Description:    This demonstrates how to hide the menubar and/or
  5. //                    the desktop.  The desktop is hidden simply by creating
  6. //                    a large background window and filling it with the
  7. //                    appropriate    pattern.  The menu bar is hidden by zeroing
  8. //                    the Menu bar height, updating the GrayRgn, and then
  9. //                    forcing all windows to redraw.  The rounded corners on
  10. //                    every screen device can also be removed using a method
  11. //                    similar to that used with the menubar.
  12. //
  13. //    Programmer:        David Hayward
  14. //                    Developer Technical Support
  15. //                    Apple Computer, Inc.
  16. //
  17. //    Environment:    Metrowerks C version 6, Universal Interfaces 2.0
  18. //
  19. //    History:        12/10/93
  20. //                      first draft
  21. //                    5/12/95
  22. //                      updated project for Metrowerks
  23. //    
  24.  
  25.  
  26. #include <Desk.h>
  27. #include <Events.h>
  28. #include <Dialogs.h>
  29. #include <LowMem.h>
  30. #include <Memory.h>
  31. #include <Windows.h>
  32. #include <QuickDraw.h>
  33. #include <ToolUtils.h>
  34.  
  35. #include "InitMac.h"
  36. #include "ShowHideMenubar.h"
  37. #include "ShowHideCorners.h"
  38.  
  39.  
  40. /**\
  41. |**| ==============================================================================
  42. |**| ENUMS
  43. |**| ==============================================================================
  44. \**/
  45. enum { kForeWind=128, kBackWind } ;
  46. enum { kMBarID=128 } ;
  47. enum { kAppleMenu=128, kFileMenu, kEditMenu, kShowHideMenu } ;
  48. enum { kNewItem=1, kOpenItem, kCloseItem, kSaveItem,
  49.         kSaveAsItem, kFileBlank1, kPageSetupItem,
  50.         kPrintItem, kFileBlank2, kQuitItem } ;
  51. enum { kShowHideMBar=1, kShowHideBack, kShowHideFore, kShowHideCorners } ;
  52.  
  53.  
  54. /**\
  55. |**| ==============================================================================
  56. |**| GLOBALS
  57. |**| ==============================================================================
  58. \**/
  59. WindowPtr    gForeWind;                /* the window pointers */
  60. WindowPtr    gBackWind;
  61. Boolean        gMBarVis = true;        /* hold the current state */
  62. Boolean        gForeVis = true; 
  63. Boolean        gBackVis = true;
  64. Boolean        gCornVis = true;
  65. Boolean        gDone = false;
  66. MenuHandle    AppleMenuHdl,            /* the menu handles */
  67.             FileMenuHdl,
  68.             EditMenuHdl,
  69.             LabelMenuHdl,
  70.             SpecialMenuHdl;
  71.  
  72.  
  73. /**\
  74. |**| ==============================================================================
  75. |**| PRIVATE FUNCTION PROTOTYPES
  76. |**| ==============================================================================
  77. \**/
  78. void    main            ( void ) ;
  79. void    SetUpMenus        ( void ) ;
  80. void    CreateWindows    ( void ) ;
  81. void    DoCommand        ( long mResult ) ;
  82. void    DoUpdate        ( WindowPtr whichWindow ) ;
  83. void    DoMouseDown        ( EventRecord event ) ;
  84. void    DoOSEvent        ( EventRecord event ) ;
  85. void    DoEventLoop        ( void );
  86.  
  87.  
  88. /**\
  89. |**| ==============================================================================
  90. |**| PRIVATE FUNCTIONS
  91. |**| ==============================================================================
  92. \**/
  93.  
  94.  
  95. /*------------------------------------------------------------------------------*\
  96.     main
  97.  *------------------------------------------------------------------------------*
  98.         a fairly normal main function.
  99. \*------------------------------------------------------------------------------*/
  100. void main ( void )
  101. {
  102.     InitToolBox( 4 ) ;
  103.     SetUpMenus() ;
  104.     CreateWindows() ;                /* create fore and back windows */
  105.     DoEventLoop() ;
  106.     SetMBarState( SHOW ) ;            /* be sure to show mbar before exiting */
  107.     SetCornersState( SHOW ) ;            /* be sure to show corners before exiting */
  108. }
  109.  
  110.  
  111. /*------------------------------------------------------------------------------*\
  112.     SetUpMenus
  113.  *------------------------------------------------------------------------------*
  114.         load in and draw the menu bar.
  115. \*------------------------------------------------------------------------------*/
  116. void SetUpMenus ( void )
  117. {
  118.     Handle        hMenuBar ;
  119.  
  120.     hMenuBar = GetNewMBar( kMBarID ) ;
  121.     SetMenuBar( hMenuBar ) ;
  122.     
  123.     AppleMenuHdl   = GetMHandle( kAppleMenu ) ;
  124.     FileMenuHdl    = GetMHandle( kFileMenu ) ;
  125.     EditMenuHdl    = GetMHandle( kEditMenu ) ;
  126.     SpecialMenuHdl = GetMHandle( kShowHideMenu ) ;
  127.     
  128.     AddResMenu( AppleMenuHdl, 'DRVR' ) ;
  129.  
  130.     DrawMenuBar() ;
  131. }
  132.  
  133.  
  134. /*------------------------------------------------------------------------------*\
  135.     CreateWindows
  136.  *------------------------------------------------------------------------------*
  137.         Create a normal window in front and a big
  138.         window in back to obscure the Finder.
  139. \*------------------------------------------------------------------------------*/
  140. void CreateWindows ( void )
  141. {
  142.     Rect        r;
  143.     RgnHandle    GrayRgn = LMGetGrayRgn() ;
  144.     
  145.     gForeWind = GetNewCWindow( kForeWind, nil, nil ) ;
  146.     gBackWind = GetNewCWindow( kBackWind, nil, nil ) ;
  147.     
  148.     /* resize the back window to cover the entire srceen. */
  149.     /* For debugging purposes I'm only making it as big as the   */
  150.     /* main sceen.  A real app would probably make it as big as  */
  151.     /* necessrary to cover all monitors.  One way to get this    */
  152.     /* rect is to Union the rect of the main screen device (i.e. */
  153.     /* screenBits.bounds - which includes the menu bar) with the */
  154.     /* bounds of the entire GrayRgn ((**GrayRgn).rgnBBox - which */
  155.     /* may not enclude the menu bar)                             */
  156.     
  157. //    r = qd.screenBits.bounds; /* main screen only - for debugging */
  158.         
  159.     UnionRect( &((**GrayRgn).rgnBBox), &(qd.screenBits.bounds), &r ) ;
  160.  
  161.     MoveWindow( gBackWind, r.left, r.top, false ) ;
  162.     SizeWindow( gBackWind, r.right-r.left, r.bottom-r.top, true ) ;
  163.     
  164.     SelectWindow( gForeWind ) ;            /* bring fore wind to front */
  165. }
  166.  
  167.  
  168. /*------------------------------------------------------------------------------*\
  169.     DoCommand
  170.  *------------------------------------------------------------------------------*
  171.         handle menu events.
  172. \*------------------------------------------------------------------------------*/
  173. void DoCommand ( long mResult )
  174. {
  175.     short        item, menu, mark ;
  176.     MenuHandle    mHandle ;
  177.     
  178.     item = LoWord( mResult ) ;
  179.     menu = HiWord( mResult ) ;
  180.     mHandle = GetMHandle( menu ) ;
  181.  
  182.     switch (menu)
  183.     {
  184.         case kShowHideMenu :                            /* if the the ShowHide menu... */
  185.             switch ( item )
  186.             {
  187.                 case kShowHideMBar :                    /* if MBar item... */ 
  188.                     SetMBarState( !GetMBarState() ) ;
  189.                     gMBarVis = !gMBarVis ;
  190.                     DoUpdate( gBackWind ) ;
  191.                     break ;
  192.                 case kShowHideBack :                    /* if Back Window item... */ 
  193.                     if ( gBackVis )
  194.                         HideWindow( gBackWind ) ;        /* show or hide the window */
  195.                     else
  196.                         ShowWindow( gBackWind ) ;
  197.                     gBackVis = !gBackVis ;                /* toggle the state global */
  198.                     break ;
  199.                 case kShowHideFore :                    /* if Fore Window item... */ 
  200.                     if ( gForeVis )
  201.                         HideWindow( gForeWind ) ;        /* show or hide the window */
  202.                     else
  203.                         ShowWindow( gForeWind ) ;
  204.                     gForeVis = !gForeVis;                /* toggle the state global */
  205.                     SelectWindow( gForeWind ) ;            /* bring fore wind to front */
  206.                     break ;
  207.                 case kShowHideCorners :                    /* if corners item... */ 
  208.                     SetCornersState( !GetCornersState() ) ;
  209.                     gCornVis = !gCornVis ;
  210.                     DoUpdate( gBackWind ) ;
  211.                     break ;
  212.             }
  213.             break;
  214.     
  215.         case kFileMenu :                                /* if the the file menu... */
  216.             if ( item==kQuitItem )                         /* if quit item... */
  217.                 gDone = true ;
  218.             break ;
  219.     }
  220.     HiliteMenu(0);                                        /* un-hilight the chosen menu item */
  221. }
  222.  
  223.  
  224. /*------------------------------------------------------------------------------*\
  225.     DoUpdate
  226.  *------------------------------------------------------------------------------*
  227.         handle update events.
  228. \*------------------------------------------------------------------------------*/
  229. void DoUpdate ( WindowPtr window )
  230. {
  231.     Str255 str;
  232.     
  233.     BeginUpdate( window ) ;
  234.     SetPort( window ) ;                                    /* make sure we're in our port */
  235.  
  236.     if ( window==gBackWind )                            /* if updating the back window */
  237.     {
  238.         Rect     r;
  239.         
  240.         /* we want to set the origin of the window to be the origin */
  241.         /* of the global coordinate system so that the pattern we */
  242.         /* draw is not offset from the desktop's pattern */
  243.         
  244.         SetOrigin(    ((GrafPtr)window)->portRect.left,
  245.                     ((GrafPtr)window)->portRect.top ) ;
  246.         
  247.         r = ((GrafPtr)window)->portRect ;
  248.  
  249.         /* for this example we'll fill the back window with same pattern */
  250.         /* as the finder's desktop but you may want to fill it else */
  251.  
  252.         if ( LMGetSPMisc2() & 0x80 )                    /* if bit 7 of this lowmem global is set then */
  253.         {                                                /* a color PixPattern is the background pat */
  254.             FillCRect( &r, GetPixPat(16) ) ;            /* so use the 'ppat'=16 resource in system */
  255.         }
  256.         else                                            /* otherwise desktop is an old-style b&w pattern */
  257.         {                                                /* in the Low Memory globals */
  258.             Pattern patt ;
  259.             LMGetDeskPattern( &patt ) ;                    /* so get the B&W pattern from LowMem  */
  260.             FillRect( &r, (ConstPatternParam)&patt ) ;    /* and fill the window with the pattern */
  261.         }
  262.         SetOrigin( 0, 0 ) ;                                /* restore the origin */
  263.     }    
  264.     if (window == gForeWind)                            /* if updating the fore window */
  265.     {
  266.         MoveTo( 50, 50 ) ;
  267.         DrawString( "\pHello World" ) ;
  268.     }
  269.     
  270.     EndUpdate( window ) ;
  271. }
  272.  
  273.  
  274. /*------------------------------------------------------------------------------*\
  275.     DoMouseDown
  276.  *------------------------------------------------------------------------------*
  277.         handle DoMouseDown events.
  278. \*------------------------------------------------------------------------------*/
  279. void DoMouseDown ( EventRecord event )
  280. {
  281.     WindowPtr   window ;
  282.     short       clickArea ;
  283.     Rect        screenRect ;
  284.  
  285.     clickArea = FindWindow( event.where, &window ) ;
  286.     screenRect = (**GetGrayRgn()).rgnBBox ;
  287.     
  288.     switch ( clickArea )
  289.     {
  290.         case inSysWindow: 
  291.             SystemClick( &event, window ) ;
  292.             break;
  293.  
  294.         case inMenuBar:                        /* handle menu selections */
  295.             DoCommand( MenuSelect(event.where) ) ;
  296.             break;
  297.  
  298.         case inDrag :                        /* handle window drags */
  299.             DragWindow( window, event.where, &screenRect ) ;
  300.             break;
  301.             
  302.         case inContent :                    /* Handle content clicks */
  303.             if ( window != FrontWindow() )    /* if click is in a back window */
  304.                 if ( window != gBackWind )    /* and its not gBackWind */
  305.                     SelectWindow( window ); /* the bring it to front */
  306.             break;
  307.             
  308.         case inGoAway :
  309.             if ( TrackGoAway(window,event.where) )
  310.                 HideWindow( window ) ;        /* hide the window */
  311.             if ( window==gForeWind )        /* if it was the fore wind */
  312.                 gForeVis = false ;            /* then update its state global */
  313.             break;
  314.     }
  315. }
  316.  
  317.  
  318. /*------------------------------------------------------------------------------*\
  319.     DoOSEvent
  320.  *------------------------------------------------------------------------------*
  321.         handle DoOSEvent events.
  322. \*------------------------------------------------------------------------------*/
  323. void DoOSEvent ( EventRecord event )
  324. {
  325.     long        mssg ;
  326.  
  327.     mssg = event.message ;
  328.     if ( (mssg>>24)==suspendResumeMessage )    /* if high byte of message indicates */
  329.     {                                          /* this is suspend/resume event */
  330.         if ( mssg & resumeFlag )                        /* if resume event */
  331.         {
  332.             /* we're switching back from another app so we need to show */
  333.             /* the menubar, the rounded corners, and the background window */
  334.             /* if the globals (gMBarVis, gCornVis, gBackVis) say we should */
  335.     
  336.             if (gBackVis)  ShowWindow(gBackWind) ;
  337.             if (!gCornVis) SetCornersState(HIDE) ;
  338.             if (!gMBarVis) SetMBarState(HIDE) ;
  339.         }
  340.         else                                            /* if suspend event */
  341.         {
  342.             /* we're switching to another app so be sure to show the menubar */
  343.             /* and the rounded corners and hide the background window  */
  344.             /* if the globals (gMBarVis, gCornVis, gBackVis) say we should */
  345.  
  346.             if (gBackVis)  HideWindow(gBackWind) ;
  347.             if (!gCornVis) SetCornersState(SHOW) ;
  348.             if (!gMBarVis) SetMBarState(SHOW) ;
  349.         }
  350.     }
  351. }
  352.  
  353.  
  354. /*------------------------------------------------------------------------------*\
  355.     DoEventLoop
  356.  *------------------------------------------------------------------------------*
  357.         Keep doing an event loop until the user quits.
  358. \*------------------------------------------------------------------------------*/
  359. void DoEventLoop ( void )
  360. {
  361.     EventRecord event ;
  362.     WindowPtr   window ;
  363.     long        mssg ;
  364.  
  365.     while ( !gDone ) 
  366.     {
  367.         if ( WaitNextEvent(everyEvent,&event,30,nil) )
  368.         {
  369.             mssg = event.message ;
  370.             switch ( event.what )
  371.             {
  372.                 case mouseDown :                        /* handle mouse clicks */
  373.                     DoMouseDown( event ) ;
  374.                     break ;
  375.                 
  376.                 case keyDown :                            /* handle key hits */
  377.                 case autoKey :
  378.                     if ( event.modifiers & cmdKey )        /* handle command keys */
  379.                         DoCommand(MenuKey( mssg & charCodeMask) ) ;
  380.                     break ;
  381.                     
  382.                 case updateEvt :                        /* handle update events */
  383.                     DoUpdate( (WindowPtr)mssg ) ;
  384.                     break ;
  385.                 
  386.                 case osEvt:                                /* handle os events */
  387.                     DoOSEvent( event ) ;
  388.                     break ;
  389.             }
  390.         }
  391.     }
  392. }
  393.  
  394.